﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using WPFServices.ViewModels;
using WPFServices.Models;
using WPFServices.Views;
using WPFServices.Views.DomainViews.BomInfo;
using WPFServices.Views.DomainViews.Process;
using WPFServices.Views.DomainViews.ProcessRouteinfo;
using WPFServices.Views.DomainViews.ProductPlaninfo;
using WPFServices.Views.DomainViews.WorkOrder;

namespace WPFServices.Models
{
    /// <summary>
    /// 主窗口的ViewModel，管理菜单和页面切换
    /// </summary>
    public class MainViewModel : BaseViewModel
    {
        private object _currentPage; // 当前显示的页面
        private MenuItem _selectedMenuItem; // 当前选中的菜单项

        /// <summary>
        /// 构造函数，初始化菜单数据
        /// </summary>
        public MainViewModel()
        {
            InitializeMenuItems();
            // 默认显示第一个页面
            if (MenuItems.Count > 0)
            {
                SelectedMenuItem = MenuItems[0];
            }
        }

        /// <summary>
        /// 菜单项集合
        /// </summary>
        public ObservableCollection<MenuItem> MenuItems { get; set; } = new ObservableCollection<MenuItem>();

        /// <summary>
        /// 当前显示的页面内容
        /// </summary>
        public object CurrentPage
        {
            get => _currentPage;
            set => SetProperty(ref _currentPage, value);
        }

        /// <summary>
        /// 当前选中的菜单项
        /// </summary>
        public MenuItem SelectedMenuItem
        {
            get => _selectedMenuItem;
            set
            {
                if (SetProperty(ref _selectedMenuItem, value))
                {
                    NavigateToPage(value?.PageName);
                }
            }
        }

        /// <summary>
        /// 初始化菜单项数据
        /// </summary>
        private void InitializeMenuItems()
        {
            MenuItems.Add(new MenuItem { Title = "首页", Icon = "🏠", PageName = "Home" });
            MenuItems.Add(new MenuItem { Title = "Bom管理", Icon = "🔐", PageName = "Bom" });
            MenuItems.Add(new MenuItem { Title = "工艺路线", Icon = "🔐", PageName = "ProcessRoute" });
            MenuItems.Add(new MenuItem { Title = "工序管理", Icon = "🔐", PageName = "Process" });
            MenuItems.Add(new MenuItem { Title = "生产计划", Icon = "📋", PageName = "ProductPlan" });
            MenuItems.Add(new MenuItem { Title = "生产工单", Icon = "📝", PageName = "WorkOrder" });
            MenuItems.Add(new MenuItem { Title = "用户管理", Icon = "👥", PageName = "User" });
            MenuItems.Add(new MenuItem { Title = "角色管理", Icon = "🔐", PageName = "Role" });
            MenuItems.Add(new MenuItem { Title = "AI助手", Icon = "🐟", PageName = "AI" });
            MenuItems.Add(new MyMenuItem { Title = "报工记录", Icon = "📋", PageName = "ReportworkRecord" });
            MenuItems.Add(new MyMenuItem { Title = "报工质检", Icon = "🔍", PageName = "ReportworkQualityInspection" });
            MenuItems.Add(new MenuItem { Title = "设置", Icon = "⚙️", PageName = "Settings" });
        }

        /// <summary>
        /// 根据页面名称导航到对应页面
        /// </summary>
        private void NavigateToPage(string pageName)
        {
            CurrentPage = pageName switch
            {
                case "Home":
                    CurrentPage = new HomeView();
                    break;
                case "Bom":
                    CurrentPage = new BomView();
                    break;
                case "ProcessRoute":
                    CurrentPage = new ProcessRouteView();
                    break;
                case "Process":
                    CurrentPage = new ProcessView();
                    break;
                case "ProductPlan":
                    CurrentPage = new ProductPlanView();
                    break;
                case "WorkOrder":
                    CurrentPage = new WorkOrderView();
                    break;
                case "User":
                    CurrentPage = new UserView();
                    break;
                case "Role":
                    CurrentPage = new WPFServices.Views.DomainViews.RoleView();
                    break;
                case "AI":
                    CurrentPage = new WPFServices.Views.DomainViews.AI.ChatView();
                    break;
                case "ReportworkRecord":
                    CurrentPage = new WPFServices.Views.DomainViews.ReportworkRecords.ReportworkRecordView();
                    break;
                case "ReportworkQualityInspection":
                    CurrentPage = new WPFServices.Views.DomainViews.ReportworkQualityInspections.ReportworkQualityInspectionView();
                    break;
                case "Settings":
                    CurrentPage = new SettingsView();
                    break;
                default:
                    CurrentPage = new HomeView();
                    break;
            }
        }
    }
}